#ifndef SW1248_H
#define SW1248_H
#include <stdint.h>
#include <pins_arduino.h>
#include "Scheduler.h"
#include "CircularBuffer.h"

void unit_test_usage_for_sw1248();
bool check_platform_is_big_endian();
typedef struct
{
   union{
        struct{
            uint8_t KB_D1: 1;
            uint8_t KB_D2: 1;
            uint8_t KB_D3: 1;
            uint8_t KB_D4: 1;
            uint8_t KB_NC: 4;
        };
        uint8_t value;
   };
}switch_keycode_t;


class SW1248 : public Scheduler
{
public:
    SW1248(uint8_t pin_kb_d1=PIN_A6,uint8_t pin_kb_d2=PIN_A7,uint8_t pin_kb_d3=7,uint8_t pin_kb_d4=8);
    virtual ~SW1248();

    void begin();
    switch_keycode_t& read();
    switch_keycode_t& state();
    void schedule() override;
    void update() override;
    void cleanup()override;
    bool changed();

    uint8_t poll();
private:
    CircularBuffer<uint8_t,10> _cb;
    uint8_t _pin_kb_d1;
    uint8_t _pin_kb_d2;
    uint8_t _pin_kb_d3;
    uint8_t _pin_kb_d4;
    switch_keycode_t _sw_last_state;
    switch_keycode_t _sw_read_state;
    #if SW1248_USE_TIMER
    volatile bool _update_flag;
    #endif
    uint32_t _counter;
    uint32_t _address_num;
};

#endif